package com.fingard.dsp.bank.directbank.cdbank01;

import com.fingard.FGBiz;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author: liuxb
 * @create: 2020-01-03 18:44
 * @version: 1.0
 **/
public class CDBANK01SinglePay extends CDBANK01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        // 返回给ATS的对象
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();//用于提取银行返回报文信息的对象
        try {
            //赋值通用参数
            CDBANK01Header header = new CDBANK01Header();
            header.ERPJnlNo = p_req1908.reqHeader.transSeqID;// ERP（财务系统）交易流水号
            header.reqTime = p_req1908.reqHeader.transTime;//指令请求时间
            header.serviceId = "DLOUTPAY";//交易码
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            writeCommonHeader(xmlTextWriter, header);
            //拼接请求报文体
            xmlTextWriter.writeElementString("REQSEQNO", header.ERPJnlNo);//凭证号,即ERP（财务系统）交易流水号
            xmlTextWriter.writeElementString("TRANTYPE", "1");//支付方式
            xmlTextWriter.writeElementString("PAYACCOUNTNO", p_req1908.singleDetail.payAct);//付款账号
            xmlTextWriter.writeElementString("RECACCOUNTNO", p_req1908.singleDetail.recAct);//收款账号
            xmlTextWriter.writeElementString("RECACCOUNTNAME", p_req1908.singleDetail.recName);//收款账号名称
            xmlTextWriter.writeElementString("TRNAMT", p_req1908.singleDetail.payAmount);//交易金额
            xmlTextWriter.writeElementString("CRYTYPE", convertCurToBank(p_req1908.singleDetail.payCur));//币种
            if ("0".equals(p_req1908.singleDetail.sameBnk)) {//他行
                xmlTextWriter.writeElementString("RECOPENBANKCODE", header.ERPJnlNo);//收款账号开户行联行网点号
                xmlTextWriter.writeElementString("RECOPENBANKNAME", header.ERPJnlNo);//收款账号开户行联行网点名称
            }
            xmlTextWriter.writeEndDocument();

            // 发送报文到银行
//        String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), header);

            //模拟银行返回
            String s = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                    "<stream>\n" +
                    "<RETCODE>AAAAAAA</RETCODE>\n" +
                    "<RETMSG>状态描述</RETMSG>\n" +
                    "<REQSEQNO>454545</REQSEQNO>\n" +
                    "</stream>\n";
            String[] bankResult = new String[]{"", s};

            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();// 接收提示信息
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {// 返回内容不为空
                    String IBSReturnCode = tmpBkReader.getFirstText("/stream/RETCODE");// 响应码
                    String IBSReturnMsg = tmpBkReader.getFirstText("/stream/RETMSG");// 响应信息
                    if ("AAAAAAA".equals(IBSReturnCode)) {

                        detail.respCode = "0";
                        detail.reqSeqID = tmpBkReader.getFirstText("/stream/RETMSG");//客户流水号
                        detail.respInfo = IBSReturnMsg;
                        detail.reqSeqID = p_req1908.singleDetail.askNumber;// 交易流水号
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = detail.transState;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = IBSReturnMsg;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            } else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }


        } catch (Exception ex) {
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        retAtsSingle.writeEnd();
        return retAtsSingle;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CDBANK01Header header = new CDBANK01Header();
            header.serviceId = "DLTRNSTT";
            //拼接请求报文头
            writeCommonHeader(xmlTextWriter, header);
            //拼接请求报文体
            xmlTextWriter.writeElementString("REQSEQNO", p_req1944.askNumber);//原交易客户流水号
            xmlTextWriter.writeEndDocument();

            // 发送报文到银行
//            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), header);

            //模拟银行返回
            String s = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                    "<stream>\n" +
                    "<RETCODE>AAAAAAA</RETCODE>\n" +
                    "<RETMSG>状态描述</RETMSG>\n" +
                    "<REQSEQNO>121212</REQSEQNO>\n" +
                    "<SRCRETCODE>121212</SRCRETCODE>\n" +
                    "<SRCRETMSG>原交易状态描述</SRCRETMSG>\n" +
                    "<STT>1</STT>\n" +
                    "</stream>\n";
            String[] bankResult = new String[]{"", s};

            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (xmlSbReader != null) {
                    String respCode = xmlSbReader.getFirstText("/stream/RETCODE");// 响应码
                    String respInfo = xmlSbReader.getFirstText("/stream/RETMSG");// 响应信息
                    if ("AAAAAAA".equals(respCode)) {//交易成功
                        detail.respCode = "0";
                        detail.reqSeqID = xmlSbReader.getFirstText("/stream/REQSEQNO");//原交易客户流水号
                        detail.transState = xmlSbReader.getFirstText("/stream/SRCRETCODE");//原交易状态码
                        detail.respInfo = xmlSbReader.getFirstText("/stream/SRCRETMSG");//原交易状态描述
                        detail.payInfoCode = detail.transState;

//                        String STT = xmlSbReader.getFirstText("/stream/STT");//原交易状态

                    } else if (respCode != null && "AAAAAAE".equals(respInfo)) {// 已提交银行处理，需稍后使用“交易状态查询”交易进行查询
                        retAtsSingle.writeRespCode("0");
                        retAtsSingle.writeRespInfo("已提交银行处理，需稍后使用“交易状态查询”交易进行查询");
                    } else if (respCode != null && "UNKNOWN".equals(respInfo)) {// 交易状态未知，使用“交易状态查询”交易进行查询
                        retAtsSingle.writeRespCode("-1");
                        retAtsSingle.writeRespInfo("交易状态未知，使用“交易状态查询”交易进行查询");
                    } else {
                        retAtsSingle.writeRespCode("-1");
                        retAtsSingle.writeRespInfo(respInfo);
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }

            } else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }


        } catch (Exception ex) {
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }

        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
